package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.DishflavorMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import com.sun.glass.ui.monocle.GLException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class DishServiceImpl implements DishService {
    @Autowired
    DishMapper dishMapper;
    @Autowired
    DishflavorMapper dishflavorMapper;
    @Autowired
    CategoryMapper categoryMapper;
    @Transactional
    public void saveWithflavor(DishDTO dish) {
        Dish dish1 = new Dish();
        BeanUtils.copyProperties(dish, dish1);
        dish1.setCreateTime(LocalDateTime.now());
        dish1.setUpdateTime(LocalDateTime.now());
        dishMapper.save(dish1);
        Long id = dish1.getId();
        if (!dish.getFlavors().isEmpty()) {
        dish.getFlavors().forEach(flavor -> {flavor.setDishId(id);});
        dishflavorMapper.save(dish.getFlavors());}
    }

    @Override
    public PageResult page(DishPageQueryDTO query) {
        PageHelper.startPage(query.getPage(),query.getPageSize());
        Page<DishVO> page=dishMapper.selectPage(query);
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    @Transactional
    public void delete(String ids) {
        String[] split = ids.split(",");
        for (String s : split) {
            if (dishMapper.selectById(Long.parseLong(s)).getStatus() != 1)//是否起售
            {
                if (categoryMapper.selectById(dishMapper.selectById(Long.parseLong(s)).getCategoryId()) != 2)//被套餐关联的不能删除
                {
                    dishMapper.deleteById(Long.valueOf(s));//删除菜品表数据
                    dishflavorMapper.deleteByDishId(ids);//删除菜品口味表数据
                }
                else throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ENABLE_FAILED);
            }else throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
        }
    }

    @Override
    public DishVO getById(String id) {
        Dish dish = dishMapper.selectById(Long.parseLong(id));
        DishVO dishVO = new DishVO();
        BeanUtils.copyProperties(dish, dishVO);
        List<DishFlavor> list=dishflavorMapper.selectbyId(Long.parseLong(id));
        dishVO.setFlavors(list);
        return dishVO;
    }

    @Override
    @Transactional
    public void update(DishDTO dish) {
        Dish dish1 = new Dish();
        BeanUtils.copyProperties(dish, dish1);
        dish1.setUpdateTime(LocalDateTime.now());
        dishMapper.update(dish1);
        dishflavorMapper.deleteByDishId(String.valueOf(dish1.getId()));
        dish.getFlavors().forEach(flavor -> {flavor.setDishId(dish1.getId());});
        if (!dish.getFlavors().isEmpty())
        {dishflavorMapper.save(dish.getFlavors());}
    }

    @Override
    public void updateStatus(Integer id, String status) {
        dishMapper.updateStatus(id,status);
    }

    @Override
    public List<Dish> listByCategory(Integer categoryId) {
        List<Dish> dishes = dishMapper.selectByCategoryId(Long.valueOf(categoryId));
        return dishes;
    }
    /**
     * 条件查询菜品和口味
     * @param dish
     * @return
     */
    public List<DishVO> listWithFlavor(Dish dish) {
        List<Dish> dishList = dishMapper.list(dish);

        List<DishVO> dishVOList = new ArrayList<>();

        for (Dish d : dishList) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(d,dishVO);

            //根据菜品id查询对应的口味
            List<DishFlavor> flavors = dishflavorMapper.selectbyId(d.getId());

            dishVO.setFlavors(flavors);
            dishVOList.add(dishVO);
        }

        return dishVOList;
    }

}
